package cn.tedu.charging.order.dao.repository.impl;

import cn.tedu.charging.order.constant.OrderStatusConstant;
import cn.tedu.charging.order.dao.mapper.OrderFailMapper;
import cn.tedu.charging.order.dao.mapper.OrderSuccessMapper;
import cn.tedu.charging.order.dao.repository.OrderRepository;
import cn.tedu.charging.order.pojo.po.ChargingBillFailPO;
import cn.tedu.charging.order.pojo.po.ChargingBillSuccessPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;

@Repository("orderRepository")
public class OrderRepositoryImpl implements OrderRepository {

    @Autowired
    private OrderSuccessMapper orderSuccessMapper;

    @Autowired
    private OrderFailMapper orderFailMapper;

    /**
     * 保存成功订单
     * @param chargingBillSuccessPO
     * @return
     */
    @Override
    public Integer saveSuccessOrder(ChargingBillSuccessPO chargingBillSuccessPO) {
        return orderSuccessMapper.insert(chargingBillSuccessPO);
    }

    @Override
    public List<ChargingBillSuccessPO> orderList(Integer userId) {
        QueryWrapper<ChargingBillSuccessPO> queryWrapper =
                new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        orderSuccessMapper.selectList(queryWrapper);
        return Collections.emptyList();
    }

    /**
     * 保存失败订单
     * @param chargingBillFailPO
     * @return
     */
    @Override
    public Integer saveFailOrder(ChargingBillFailPO chargingBillFailPO) {
        return orderFailMapper.insert(chargingBillFailPO);
    }

    @Override
    public Integer updateOrderStatusFromProcess2ExceptionEnd(String orderNo) {
        //UpdateWrapper 条件
        UpdateWrapper<ChargingBillSuccessPO> updateWrapper =
                new UpdateWrapper<>();
        updateWrapper.eq("bill_Id", orderNo);
        updateWrapper.eq("bill_status", OrderStatusConstant.ORDER_STATUS_PROGRESS);

        //目标
        ChargingBillSuccessPO chargingBillSuccessPO = new ChargingBillSuccessPO();
        chargingBillSuccessPO.setBillStatus(OrderStatusConstant.ORDER_STATUS_EXCEPTION_END);
        //update set bill_status = 3 where bill_Id = orderNo and bill_status = 1
        return orderSuccessMapper.update(chargingBillSuccessPO,updateWrapper);
    }

    @Override
    public ChargingBillSuccessPO getSuccess(String orderNo) {
        QueryWrapper<ChargingBillSuccessPO> queryWrapper =
                new QueryWrapper<>();
        queryWrapper.eq("bill_id", orderNo);
        return orderSuccessMapper.selectOne(queryWrapper);
    }

    @Override
    public ChargingBillFailPO getFail(String orderNo) {
        QueryWrapper<ChargingBillFailPO> queryWrapper =
                new QueryWrapper<>();
        queryWrapper.eq("bill_id", orderNo);
        return orderFailMapper.selectOne(queryWrapper);
    }

    @Override
    public void updateSuccessOrder(String orderNo, int gunId) {
        //UpdateWrapper 条件
        UpdateWrapper<ChargingBillSuccessPO> updateWrapper =
                new UpdateWrapper<>();
        updateWrapper.eq("bill_Id", orderNo);
        //目标
        ChargingBillSuccessPO chargingBillSuccessPO = new ChargingBillSuccessPO();
        chargingBillSuccessPO.setGunId(gunId);
        //update set gunId = xx where bill_Id = orderNo
        orderSuccessMapper.update(chargingBillSuccessPO,updateWrapper);
    }

    @Override
    public Integer updateOrderStatusFromProcess2NormalEnd(String orderNo) {
        UpdateWrapper<ChargingBillSuccessPO> updateWrapper =
                new UpdateWrapper<>();
        updateWrapper.eq("bill_Id", orderNo);
        updateWrapper.eq("bill_status", OrderStatusConstant.ORDER_STATUS_PROGRESS);
        //目标
        ChargingBillSuccessPO chargingBillSuccessPO = new ChargingBillSuccessPO();
        chargingBillSuccessPO.setBillStatus(OrderStatusConstant.ORDER_STATUS_NORMAL_END);
        //update set bill_status = 3 where bill_Id = orderNo and bill_status = 1
        return orderSuccessMapper.update(chargingBillSuccessPO,updateWrapper);
    }

}
